package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.utils.ChoosePay;
import cn.wikiflyer.beikang.act.user.utils.ChoosePay.ChooseListener;
import cn.wikiflyer.beikang.bean.MyOrderListBean.GoodBean;
import cn.wikiflyer.beikang.bean.MyOrderListBean.MyOrderBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.payali.PaymentObject;
import cn.wikiflyer.beikang.payali.PaymentObject.OnPayListener;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.WKWebAct;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 我的订单详情
 * 
 * @author qunlee_mr
 * 
 */
public class UserOrderInfoAct extends BaseActivity {

	@ViewInject(id = R.id.header)
	private WKHeader header;
	@ViewInject(id = R.id.orderinfo_number)
	private TextView numberText;
	@ViewInject(id = R.id.orderinfo_time)
	private TextView timeText;
	@ViewInject(id = R.id.orderinfo_username)
	private TextView usernameText;
	@ViewInject(id = R.id.orderinfo_phone)
	private TextView phoneCallText;
	@ViewInject(id = R.id.orderinfo_address)
	private TextView addressText;
	@ViewInject(id = R.id.orderinfo_paytype)
	private TextView payTypeText; // 支付方式
	@ViewInject(id = R.id.orderinfo_money)
	private TextView payMoneyText; // 支付金额
	@ViewInject(id = R.id.orderinfo_conveycompany)
	private TextView conveyCompanyText; // 物流公司
	@ViewInject(id = R.id.orderinfo_conveycode)
	private TextView conveyCodeText; // 物流单号
	@ViewInject(id = R.id.orderinfo_goodesum)
	private TextView goodMoneyText; // 商品金额金额
	@ViewInject(id = R.id.orderinfo_conveymoney)
	private TextView conveyMoneyText; // 运费
	@ViewInject(id = R.id.orderinfo_total)
	private TextView totalText; // 总额

	@ViewInject(id = R.id.orderinfo_goodlist_linear)
	private LinearLayout goodlist_linear;

	// 支付显示
	@ViewInject(id = R.id.ll_01)
	private LinearLayout ll_01;
	// 查看确认显示
	@ViewInject(id = R.id.ll_02)
	private LinearLayout ll_02;
	// btn0 ： 去支付
	@ViewInject(id = R.id.orderinfo_btn0, click = "onClick")
	private Button btn0;
	// btn1 ： 查物流 和 取消订单
	@ViewInject(id = R.id.orderinfo_btn1, click = "onClick")
	private TextView btn1;
	// btn2 ： 确认收货
	@ViewInject(id = R.id.orderinfo_btn2, click = "onClick")
	private Button btn2;

	private String orderNo; // 订单编号
	private String orderDescription; // 订单详情

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
		orderNo = getIntent().getStringExtra("orderno");
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_order_info);
	}

	@Override
	public void init() {
		Log.i("", "orderNo" + orderNo);
		System.out.println("orderNo" + orderNo);

	}

	@Override
	public void setData() {
		new GetOrderInfoTask(ctx);
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.orderinfo_btn2:// 确认收货
			new ComfirmReceiptTask(ctx);
			break;
		case R.id.orderinfo_btn1:// 查看物流
			String no = base.data.kuaidi_no;
			String comp = base.data.kuaidi_company;
			if (no == null || no.equals("")) {
				IApplication.showToast("快递单号异常");
				break;
			}
			Intent i = new Intent(this, WKWebAct.class);
			i.putExtra("url", "http://m.kuaidi100.com/index_all.html?type="
					+ comp + "&postid=" + no);
			i.putExtra("title", "快递查询");
			startActivity(i);
			break;
		case R.id.orderinfo_btn0:// 支付
			pay();
			break;
		default:
			break;
		}
	}

	/**
	 * 未付款的支付方法
	 */
	private void pay() {
		payAlipay();
//		ChoosePay choosePay = new ChoosePay(UserOrderInfoAct.this);
//		choosePay.setChooseListener(new ChooseListener() {
//			@Override
//			public void choose() {		//支付宝
//				payAlipay();
//			}
//		}, new ChooseListener() {
//			@Override
//			public void choose() {		//微信
//				Toast.makeText(ctx, "获取微信prepay_id", Toast.LENGTH_SHORT).show();
//			}
//		}, new ChooseListener() {
//			@Override
//			public void choose() {		//银联
//				Toast.makeText(ctx, "获取银联tn", Toast.LENGTH_SHORT).show();
//			}
//		});
//		choosePay.showSelection(header);
	}

	/** 支付宝支付
	 */
	private void payAlipay() {
		PaymentObject mPayObject = PaymentObject.getInstance(activity);
		mPayObject.setOnPayListener(new OnPayListener() {
			@Override
			public void success() {
				// ctx.startActivity(mIntent);
				finish();
			}
			@Override
			public void failed() {
				// 不显示失败窗口
				// new AliPayPopWindow(ctx).show();
			}
		});
		mPayObject.setOrderno(orderNo);
		String money = base.data.total;
		if (IApplication.isPayTestMoney) {
			money = "0.01";
		}
		mPayObject.getOrderInfo("订单号:" + orderNo, "订单详情:" + orderDescription,money);
		mPayObject.Pay();
	}

	/**
	 * 确认收货接口
	 */
	class ComfirmReceiptTask extends WKAsyncTaskPro {
		public ComfirmReceiptTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.confirmReceipt("" + orderNo);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				activity.finish();
				IApplication.showToast("收货成功");
			} else {
				Toast.makeText(ctx, "" + base.status + " - " + base.info, 0)
						.show();
			}
		}
	}

	/**
	 * 获取订单详情
	 */
	class GetOrderInfoTask extends WKAsyncTaskPro {
		public GetOrderInfoTask(Context context) {
			super(context, true);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getOrderInfo("" + orderNo, isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<MyOrderBean>>() {
			}.getType();
			Gson gson = new Gson();
			base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				// Toast.makeText(ctx, base.status+" - "+base.info, 0).show();
				orderDescription = base.data.toString();
				updateUI(base.data);
				btn0.setClickable(true);
			} else {
				Toast.makeText(ctx, "获取数据失败" + base.info, 0).show();
				btn0.setClickable(false);
			}
		}
	}

	private BKBaseBean<MyOrderBean> base;

	/**
	 * 用网络数据刷新界面
	 */
	private void updateUI(MyOrderBean bean) {
		if (bean.pay_status == 0) {// 未付款
			ll_01.setVisibility(View.VISIBLE);
			ll_02.setVisibility(View.GONE);
		} else if (bean.pay_status == 1) {// 已付款
			ll_01.setVisibility(View.GONE);
			if (bean.is_receipt.equals("1")) {// 已收货
				ll_02.setVisibility(View.GONE);
			} else {// 未收货
				ll_02.setVisibility(View.VISIBLE);
			}
		}
		// else {
		// ll_01.setVisibility(View.GONE);
		// ll_02.setVisibility(View.GONE);
		// }
		fillTextData(numberText, bean.orderno);
		fillTextData(timeText, WKTimeUtils.formatTime(bean.ordertime) + "");
		fillTextData(usernameText, bean.receiver);
		fillTextData(phoneCallText, bean.phone);
		fillTextData(addressText, bean.address);
		fillTextData(payTypeText, (bean.pay_type == 0 ? "支付宝" : "微信"));
		fillTextData(payMoneyText, bean.total);
		fillTextData(conveyCompanyText, bean.kuaidi_company);
		fillTextData(conveyCodeText, bean.kuaidi_no);
		fillTextData(goodMoneyText, bean.total);
		fillTextData(conveyMoneyText, "0"); // 运费
		fillTextData(totalText, "￥" + bean.total); //
		goodlist_linear.removeAllViews();
		for (GoodBean mGoodBean : bean.goods) {
			View goodView = LayoutInflater.from(ctx).inflate(
					R.layout.bk_user_order_info_item_lay, null);
			initGiftList(mGoodBean, goodView);
			goodlist_linear.addView(goodView);
		}
	}

	// 初始化物品清单
	private void initGiftList(GoodBean goodBean, View view) {
		app().imageLoader.displayImage("" + goodBean.thumb,
				((ImageView) view.findViewById(R.id.imageView_logo)),
				app().options);
		((TextView) view.findViewById(R.id.textView_name)).setText(""
				+ goodBean.title);
		((TextView) view.findViewById(R.id.textView_sum)).setText("￥"
				+ goodBean.unit_price);
		((TextView) view.findViewById(R.id.textView_num)).setText("x"
				+ goodBean.quantity);
	}

	/**
	 * 填充数据
	 */
	private void fillTextData(TextView textView, String fillData) {
		textView.setText(fillData);
	}

}
